//
//  RecipeViewController.m
//  掌厨
//
//  Created by aoyolo on 15/10/22.
//  Copyright (c) 2015年 李祖铜. All rights reserved.
//

#import "RecipeViewController.h"


#import "AFNetworking.h"
//#import "UIImageView+AFNetworking.h"


#import "HeadCell.h"
#import "ClassifyCell.h"
#import "HotCell.h"
#import "NewProductCell.h"
#import "FoodCell.h"
#import "ListCell.h"

#import "Headmodel.h"

@interface RecipeViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_imgUrl;
    NSMutableArray *_classifys;
    NSMutableArray *_hosts;
    NSMutableArray *_newProducts;
    NSMutableArray *_foods;
    NSMutableArray *_lists;

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation RecipeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imgUrl = [NSMutableArray array];
    

    //设置左边导航栏扫一扫按钮
    [self leftButton];
    
    //设置右边导航栏消息按钮
    [self rightButton];
    
    self.navigationController.navigationBar.barTintColor =appColor;
    
    //搜索框
    [self searchBox];
    
    //设置tableView唯一尾视图
    [self tailViewText];
    
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"HeadCell" bundle:nil] forCellReuseIdentifier:@"HeadCellId"];
    [_tableView registerNib:[UINib nibWithNibName:@"ClassifyCell" bundle:nil] forCellReuseIdentifier:@"ClassifyCellId"];
    [_tableView registerNib:[UINib nibWithNibName:@"HotCell" bundle:nil] forCellReuseIdentifier:@"HotCellId"];
    [_tableView registerNib:[UINib nibWithNibName:@"NewProductCell" bundle:nil] forCellReuseIdentifier:@"NewProductCellId"];
    [_tableView registerNib:[UINib nibWithNibName:@"FoodCell" bundle:nil] forCellReuseIdentifier:@"FoodCellId"];
    [_tableView registerNib:[UINib nibWithNibName:@"ListCell" bundle:nil] forCellReuseIdentifier:@"ListCellId"];
    
    
    
    
    //Post网络请求
    AFHTTPRequestOperationManager *mag = [AFHTTPRequestOperationManager manager];
    [mag POST:zhangchuURL parameters:@{@"methodName":@"HomeIndex",@"user_id":@"0",@"version":@"1.0"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //图片广告滚动数据
        NSArray *arr = responseObject[@"data"][@"banner"][@"data"];
        for (NSDictionary *dic in arr)
        {
            Headmodel *model = [[Headmodel alloc]init];
            model.title = dic[@"title"];
            model.image = dic[@"image"];
            model.link = dic[@"link"];
            [_imgUrl addObject:model];

        }
        //分类数据
        _classifys = responseObject[@"data"][@"category"][@"data"];
        //热门推荐数据
        _hosts = responseObject[@"data"][@"data"][0][@"data"];
        //新品推荐数据
        _newProducts = responseObject[@"data"][@"data"][1][@"data"];
        //美食专题
        _foods = responseObject[@"data"][@"data"][3][@"data"];
        //排行榜
        _lists = responseObject[@"data"][@"data"][2][@"data"];
        
        [_tableView reloadData];//刷新网络数据
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
}


#pragma mark- UITableViewDelegate
//段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

//每一段有一行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

//tableView的行高
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0)
    {
        return 150;
    }
    else if (indexPath.section == 1)
    {
        return 200;
    }
    else if (indexPath.section == 2)
    {
        return 350;
    }
    else if (indexPath.section == 3)
    {
        return 180;
    }
    else if (indexPath.section == 4)
    {
        return 440;
    }
    else if (indexPath.section == 5)
    {
        return 230;
    }
    return 0;
}
//段与段的间距
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    return 10.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        HeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadCellId"];
        [cell setImg:_imgUrl];
        return cell;
    }
    else if (indexPath.section ==1)
    {
        ClassifyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassifyCellId"];
        [cell setFile:_classifys];
        return cell;
    }
    else if (indexPath.section == 2)
    {
        HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotCellId"];
        [cell setHot:_hosts];
        return cell;
    }
    else if (indexPath.section == 3)
    {
        NewProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewProductCellId"];
        [cell setNewProduct:_newProducts];
        return cell;
    }
    else if (indexPath.section == 4)
    {
        FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodCellId"];
        [cell setFood:_foods];
        return cell;
    }
    else if (indexPath.section == 5)
    {
        ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCellId"];
        [cell setList:_lists];
        return cell;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //进行效果的反选
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//反选时调用的代理
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


-(void)tailViewText
{
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    //设置tableView的尾视图，有且只有一个
    _tableView.tableFooterView = view2;
    view2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *labeltext1 = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 160, 20)];
    labeltext1.font=[UIFont systemFontOfSize:15];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:@"给个好评，鼓励一下"];
    NSRange contentRange = {0, [content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    labeltext1.attributedText = content;
    [view2 addSubview:labeltext1];
    
    
    UILabel *labeltext2 = [[UILabel alloc]initWithFrame:CGRectMake(160, 160, 100, 20)];
    labeltext2.font = [UIFont systemFontOfSize:14];
    labeltext2.text = @"掌厨007";
    labeltext2.textColor = [UIColor grayColor];
    [view2 addSubview:labeltext2];
}









-(void)leftButton
{
    UIButton *sweepBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sweepBtn.frame= CGRectMake(0, 0, 50, 35);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sweepBtn];
    [sweepBtn setImage:[UIImage imageNamed:@"扫一扫"] forState:UIControlStateNormal];
    sweepBtn.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, -sweepBtn.titleLabel.bounds.size.width-50);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
    
    [sweepBtn setTitle:@"扫一扫" forState:UIControlStateNormal];
    sweepBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    sweepBtn.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
    [sweepBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
    [sweepBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体
    sweepBtn.titleEdgeInsets = UIEdgeInsetsMake(0, sweepBtn.titleLabel.bounds.size.width, -10, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
    
    [sweepBtn setContentEdgeInsets:UIEdgeInsetsMake(10, 0, 0, 0)];
    sweepBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
    
    [sweepBtn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
}

- (void)rightButton
{
    UIButton *newsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    newsBtn.frame= CGRectMake(0, 0, 50, 35);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:newsBtn];
    [newsBtn setImage:[UIImage imageNamed:@"消息默认"] forState:UIControlStateNormal];
    newsBtn.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, newsBtn.titleLabel.bounds.size.width);
    
    [newsBtn setTitle:@"消息" forState:UIControlStateNormal];
    newsBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    newsBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [newsBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [newsBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    newsBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -newsBtn.titleLabel.bounds.size.width-40, -10, 0);
    
    [newsBtn setContentEdgeInsets:UIEdgeInsetsMake(10, 0, 0, 0)];
    newsBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    [newsBtn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
}

-(void)searchBox
{
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 200, 25)];
    searchView.backgroundColor = [UIColor whiteColor];
    searchView.layer.masksToBounds = YES;
    searchView.layer.cornerRadius =7;
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchView.bounds];
    searchBar.placeholder = @"输入菜名或食材搜索";
    [searchView addSubview:searchBar];
    
    self.navigationItem.titleView = searchView;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{//点击屏幕对所有键盘返回消失
    [self.view endEditing:YES];
}


@end
